package de.clapp.musicbox.loader;

import java.util.ArrayList;

import android.content.Context;
import android.support.v4.content.AsyncTaskLoader;
import de.clapp.CLAppModel;
import de.clapp.Settings;
import de.clapp.backend.network.ClientRequest;
import de.clapp.backend.network.TCPSocket;
import de.clapp.backend.requests.LastPlayedSongsRequest;
import de.clapp.backend.requests.MusicGetCurrentTimeRequest;
import de.clapp.data.Club;
import de.clapp.data.MusicItem;

public class MusicPlayedSongsLoader extends AsyncTaskLoader<ArrayList<MusicItem>>
{
//    private LastPlayedSongsRequest request;
    
    private int currentPosition;
    
    public int getCurrentPosition()
    {
        return this.currentPosition;
    }
    
    public MusicPlayedSongsLoader(Context context)
    {
        super(context);
    }

//    public LastPlayedSongsRequest getRequest()
//    {
//        return this.request;
//    }
    
    @Override
    public ArrayList<MusicItem> loadInBackground()
    {
        LastPlayedSongsRequest lastSongRequest = new LastPlayedSongsRequest();
        MusicGetCurrentTimeRequest curRequest = new MusicGetCurrentTimeRequest();
        ArrayList<MusicItem> songList = null;

        try
        {
            Club club = CLAppModel.getInstance().getCurrentClub();
            ClientRequest cr = new ClientRequest(new TCPSocket(club.getIpAddress(), club.getPort()));
            
            cr.request(lastSongRequest);
            songList = lastSongRequest.getItem();
            cr.request(curRequest);
            this.currentPosition = curRequest.getItem();
            
            cr.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return songList;
    }
}
